package com.lhd.mpaas.demo.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alipay.mobile.framework.LauncherApplicationAgent;
import com.alipay.mobile.h5container.api.H5Param;
import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lhd.mpaas.demo.R;
import com.lhd.mpaas.demo.adapter.HomeMenuAdapter;
import com.lhd.mpaas.demo.base.BaseLazyFragment;
import com.lhd.mpaas.demo.model.HomeMenuBean;
import com.lhd.mpaas.demo.utils.H5Util;
import com.mpaas.mas.adapter.api.MPLogger;
import com.mpaas.nebula.adapter.api.MPNebula;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseLazyFragment {


    @BindView(R.id.rv_menu)
    RecyclerView mRvMenu;

    private HomeMenuAdapter mHomeMenuAdapter;
    private List<HomeMenuBean> homeMenuBeanList = new LinkedList<>();

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onInitView() {
        super.onInitView();

        mHomeMenuAdapter = new HomeMenuAdapter(mActivity, R.layout.item_home_menu, homeMenuBeanList);
        mHomeMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                String url = "https://mcube-prod.oss-cn-hangzhou.aliyuncs.com/226BA46061731-default/66666666/1.0.0.0_all/nebula/fallback/index.html";
                String url = "https://www.baidu.com";

                if (position == 0) {
                    H5Util.openH5(url);

                    // logId 描述当前埋点的关键词，控制台自定义分析中事件id对应该字段
                    // logId 不能为空
                    String logId = "OpenH5";

                    // bizType 标识业务类型，相同 bizType 的自定义日志将遵循同样的上报策略，存储在同一个文件中，文件名为进程名_bizType
                    // bizType 可以为空，默认为 userbehavor
                    String bizType = "SwitchPage";

                    // params 为自定义参数，控制台自定义分析中事件的自定义属性对应这些键值对
                    // params 可以为空
                    Map<String, String> params = new HashMap<>();
                    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateStr = dateformat.format(System.currentTimeMillis());
                    params.put("current_time", dateStr);

                    // 可调用多个重载方法
                    // MPLogger.event(logId);
                    // MPLogger.event(logId, bizType);
                    // MPLogger.event(logId, params);
                    MPLogger.event(logId, bizType, params);

                } else if (position == 1) {

                    Bundle bundle = new Bundle();
                    bundle.putString(H5Param.LONG_URL, url);
                    bundle.putBoolean(H5Param.LONG_SHOW_TITLEBAR, true);
                    bundle.putBoolean(H5Param.LONG_SHOW_PROGRESS, true);
                    bundle.putBoolean(H5Param.LONG_CAN_PULL_DOWN, true);
                    bundle.putString(H5Param.LONG_LANDSCAPE, "auto");
                    bundle.putString(H5Param.TITLE, "默认标题");
                    bundle.putString(H5Param.DEFAULT_TITLE, "默认标题2");
                    bundle.putString(H5Param.FAV_TITLE, "默认标题1");
                    MPNebula.startUrl(url, bundle);
//                    MPNebula.startUrl("https://tech.antfin.com/");
                } else if (position == 2) {
                    // 启动小程序 demo
                    String appId = "2019111412345678"; // 您的小程序的 appId;
                    Bundle bundle = new Bundle();
                    // 不设置就跳转发布小程序时的默认首页
                    // bundle.putString(H5Param.PAGE, "跳转的页面路径");
                    LogUtils.d("==========>>>" + "打开小程序");
                    LauncherApplicationAgent.getInstance().getMicroApplicationContext().startApp(null, appId, bundle);
                } else if (position == 3) {
                    // 启动小程序 demo
                    MPNebula.startApp("2019111412345678");

                    // logId 描述当前埋点的关键词，控制台自定义分析中事件id对应该字段
                    // logId 不能为空
                    String logId = "OpenApplets";

                    // bizType 标识业务类型，相同 bizType 的自定义日志将遵循同样的上报策略，存储在同一个文件中，文件名为进程名_bizType
                    // bizType 可以为空，默认为 userbehavor
                    String bizType = "SwitchPage";

                    // params 为自定义参数，控制台自定义分析中事件的自定义属性对应这些键值对
                    // params 可以为空
                    Map<String, String> params = new HashMap<>();
                    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateStr = dateformat.format(System.currentTimeMillis());
                    params.put("current_time", dateStr);

                    // 可调用多个重载方法
                    // MPLogger.event(logId);
                    // MPLogger.event(logId, bizType);
                    // MPLogger.event(logId, params);
                    MPLogger.event(logId, bizType, params);
                } else if (position == 4) {
                    // 启动 h5 离线包 kylin-demo
                    MPNebula.startApp("20200409");
                } else if (position == 5) {
                    // 启动 h5 离线包 ionic1-hwork

                    Bundle params = new Bundle();
                    params.putString("userID", "A0000001");
                    params.putString("token", "A0000001token==");
                    params.putString("appId", "20200421");
                    params.putString("url", "/www/main.html#/ScanQuery");
                    MPNebula.startApp("20200421");
                } else if (position == 6) {
                    // 启动 h5 离线包 ionic1-hwork

                    Bundle params = new Bundle();
                    params.putString("userID", "A0000001");
                    params.putString("token", "A0000001token==");
                    params.putString("appId", "20200421");
                    params.putString("url", "/www/main.html#/ScanQuery");
                    MPNebula.startApp("20200421", params);
                }
            }
        });
        mHomeMenuAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                LogUtils.d("setOnItemLongClickListener===============>>>" + position);
                return false;
            }
        });
        mHomeMenuAdapter.notifyDataSetChanged();

        mRvMenu.setLayoutManager(new GridLayoutManager(mActivity, 4));
        mRvMenu.setAdapter(mHomeMenuAdapter);
        mRvMenu.setNestedScrollingEnabled(false);
        // mRvMenu.setHasFixedSize(true);
    }

    @Override
    protected void onInitData() {
        super.onInitData();

        homeMenuBeanList.clear();
        homeMenuBeanList.add(new HomeMenuBean("打开h5页面", "ceshicaidan", "file:///android_asset/demo.html"));
        homeMenuBeanList.add(new HomeMenuBean("打开h5页面", "ceshicaidan", "file:///android_asset/demo.html"));
        homeMenuBeanList.add(new HomeMenuBean("打开小程序", "ceshicaidan", "file:///android_asset/demo.html"));
        homeMenuBeanList.add(new HomeMenuBean("打开小程序", "ceshicaidan", "file:///android_asset/demo.html"));
        homeMenuBeanList.add(new HomeMenuBean("打开H5离线包", "ceshicaidan", "file:///android_asset/demo.html"));
        homeMenuBeanList.add(new HomeMenuBean("打开Hwork", "ceshicaidan", "file:///android_asset/demo.html"));
        homeMenuBeanList.add(new HomeMenuBean("打开Hwork", "ceshicaidan", "file:///android_asset/demo.html"));

        int i = 44;
        while (i > 0) {
            homeMenuBeanList.add(new HomeMenuBean("测试菜单" + i, "ceshicaidan", "file:///android_asset/demo.html"));
            i--;
        }
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//        unbinder1 = ButterKnife.bind(this, rootView);
//        return rootView;
//    }
//
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        unbinder1.unbind();
//    }
}
